package com.six.controller;

import com.six.domain.vo.DishSaleRankVo;
import com.six.domain.vo.PayChannelStatVo;
import com.six.domain.vo.SalesStatVo;
import com.six.service.ReportService;
import com.six.util.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@RestController
@RequestMapping("/api/manager/report")
@RequiredArgsConstructor
public class ReportController {

    private final ReportService reportService;

    /**
     * 销售统计
     * GET /api/manager/report/sales?start=2025-08-01&end=2025-08-31
     */
    @GetMapping("/sales")
    @PreAuthorize("hasRole('MANAGER')")
    public Result<List<SalesStatVo>> sales(@RequestParam Long storeId,
                                           @RequestParam String start,
                                           @RequestParam String end) {
        LocalDateTime s = LocalDate.parse(start).atStartOfDay();
        LocalDateTime e = LocalDate.parse(end).atTime(LocalTime.MAX);
        return Result.ok(reportService.salesStat(storeId, s, e));
    }

    /**
     * 菜品销量排行
     * GET /api/manager/report/dish-rank?start=2025-08-01&end=2025-08-31&top=10
     */
    @GetMapping("/dish-rank")
    @PreAuthorize("hasRole('MANAGER')")
    public Result<List<DishSaleRankVo>> dishRank(@RequestParam Long storeId,
                                                 @RequestParam String start,
                                                 @RequestParam String end,
                                                 @RequestParam(defaultValue = "10") int top) {
        LocalDateTime s = LocalDate.parse(start).atStartOfDay();
        LocalDateTime e = LocalDate.parse(end).atTime(LocalTime.MAX);
        return Result.ok(reportService.dishRank(storeId, s, e, top));
    }

    /**
     * 支付渠道统计
     * GET /api/manager/report/pay-channel?start=2025-08-01&end=2025-08-31
     */
    @GetMapping("/pay-channel")
    @PreAuthorize("hasRole('MANAGER')")
    public Result<List<PayChannelStatVo>> payChannel(@RequestParam Long storeId,
                                                     @RequestParam String start,
                                                     @RequestParam String end) {
        LocalDateTime s = LocalDate.parse(start).atStartOfDay();
        LocalDateTime e = LocalDate.parse(end).atTime(LocalTime.MAX);
        return Result.ok(reportService.payChannel(storeId, s, e));
    }
}